<?php
if(!defined('IN_MANU')) { die('[ERROR] You cannot load this page directly !!!'); }
/**
 * Router class
 *
 * @package    application
 *
 */

class Router
{
	private $registry;

	private $path;

	private $args = array();

	private $arg = null;
	private $arg2 = null;

	public $file;

	public $controller;

	public $action;

	function __construct($registry)
	{
		$this->registry = $registry;
	}

	public function getRoot() {
		$host = 'http://'.$_SERVER['HTTP_HOST'];
		$directory = dirname($_SERVER['SCRIPT_NAME']);
		$root = $directory == '\\' ? $host.'/' : $host.$directory.'/';
		return $root;
	}

	/**
	 * set controller dir path
	 */
	public function setPath($path)
	{
		// check path
		if ( is_dir($path) == false )
		{
			throw new Exception ('Invalid controller path: "' . $path . '"');
		}
		// set path
		$this->path = $path;
	}


	/**
	 * load the controller
	 */
	public function loader()
	{
		//$user = 'test';
		

		$user = $this->registry->db->get('userManager')->getCurrentUser();
		
		// get controller
		$this->getController($user);

		// check file
		if (is_readable($this->file) == false){
			$this->file = $this->path . '/errorController.php';
			$this->controller = 'error';
		}

		// include the controller
		include $this->file;

		// create a new controller object
		$class = $this->controller . 'Controller';
		$controller = new $class($this->registry);

		// check if the action is callable
		if ( is_callable(array($controller, $this->action)) == false ){
			$action = 'index';
		}
		else {
			$action = $this->action;
		}
			
		// run the controller
		if ( isset($this->arg2) && ($this->arg2 != null) ) {
			$controller->$action($this->arg, $this->arg2);
		}
		else if( isset($this->arg) && ($this->arg != null)) {
			$controller->$action($this->arg);
		}
		else {
			$controller->$action();
		}
	}


	/**
	 * get the controller
	 */
	private function getController($user)
	{
		//$user = $this->registry->db->get('userManager')->getCurrentUser();
		if($user == null){
			// check login
			$this->controller = 'login';
			$this->action = 'index';
		}
		else {
			$route = (empty($_GET['act'])) ? '' : $_GET['act'];

			if ( empty($route) ){
				$route = 'index';
			}
			else {
				// explode the URL parameters
				$parts = explode('/', $route);
				$this->controller = $parts[0];
				$cnt = count($parts);
				
				if($cnt == 1){
					$this->arg = null;
					$this->arg2 = null;
					$this->action = 'index';
				}
				else if ($cnt ==  2){
					$this->arg = null;
					$this->arg2 = null;
					$this->action = $parts[1];
				}
				else {
					if (isset($parts[1])){
						$this->action = $parts[1];
					}
					if (isset($parts[2])){
						$this->arg = $parts[2];
					}
					if (isset($parts[3])) {
						$this->arg2 = $parts[3];
					}
// 					echo 'act = ' . $this->action . 'arg = ' . $this->arg . 'arg2 = ' . $this->arg2;
				}
			}

			if ( empty($this->controller) ){
				$this->controller = 'index';
			}

			if ( empty($this->action) ){
				$this->action = 'index';
			}

			/*
				$userType = $user->get('type');
			if(($userType == 'Normal' && !in_array($this->controller, array('dashboard','book', 'login', 'logout', 'index', 'cron', 'dbbackup')))
			|| ($userType != 'Normal' && $userType != 'Admin')){
			$this->controller = 'error';
			$this->action = 'index';
			}
			*/
		}

		// set the controller path
		$this->file = $this->path .'/'. $this->controller . 'Controller.php';
	}
}

?>